{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这个文件中, 我们将创建一个由langgraph支持的chatboot, 功能如下:\n",
    "- 通过网络搜索来回答一般的问题\n",
    "- 通过调用来维持会话状态\n",
    "- 路由复杂查询给人类看\n",
    "- 使用自定义状态来控制其行为\n",
    "- 回放并探索替代对话路径\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://mirrors.aliyun.com/pypi/simple/\n",
      "Requirement already satisfied: langgraph in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (0.2.74)\n",
      "Requirement already satisfied: langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langgraph) (0.3.39)\n",
      "Requirement already satisfied: langgraph-checkpoint<3.0.0,>=2.0.10 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langgraph) (2.0.16)\n",
      "Requirement already satisfied: langgraph-sdk<0.2.0,>=0.1.42 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langgraph) (0.1.53)\n",
      "Requirement already satisfied: langsmith<0.4,>=0.1.125 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (0.3.11)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (9.0.0)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (1.33)\n",
      "Requirement already satisfied: PyYAML>=5.3 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (6.0.2)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (24.2)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (4.12.2)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.5.2 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (2.10.6)\n",
      "Requirement already satisfied: msgpack<2.0.0,>=1.1.0 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langgraph-checkpoint<3.0.0,>=2.0.10->langgraph) (1.1.0)\n",
      "Requirement already satisfied: httpx>=0.25.2 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langgraph-sdk<0.2.0,>=0.1.42->langgraph) (0.28.1)\n",
      "Requirement already satisfied: orjson>=3.10.1 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langgraph-sdk<0.2.0,>=0.1.42->langgraph) (3.10.15)\n",
      "Requirement already satisfied: anyio in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (4.8.0)\n",
      "Requirement already satisfied: certifi in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (2025.1.31)\n",
      "Requirement already satisfied: httpcore==1.* in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (1.0.7)\n",
      "Requirement already satisfied: idna in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (3.10)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from httpcore==1.*->httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (0.14.0)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (3.0.0)\n",
      "Requirement already satisfied: requests<3,>=2 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langsmith<0.4,>=0.1.125->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (2.32.3)\n",
      "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langsmith<0.4,>=0.1.125->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (1.0.0)\n",
      "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from langsmith<0.4,>=0.1.125->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (0.23.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from pydantic<3.0.0,>=2.5.2->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.2 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from pydantic<3.0.0,>=2.5.2->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (2.27.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from requests<3,>=2->langsmith<0.4,>=0.1.125->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (3.4.1)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from requests<3,>=2->langsmith<0.4,>=0.1.125->langchain-core!=0.3.0,!=0.3.1,!=0.3.10,!=0.3.11,!=0.3.12,!=0.3.13,!=0.3.14,!=0.3.15,!=0.3.16,!=0.3.17,!=0.3.18,!=0.3.19,!=0.3.2,!=0.3.20,!=0.3.21,!=0.3.22,!=0.3.3,!=0.3.4,!=0.3.5,!=0.3.6,!=0.3.7,!=0.3.8,!=0.3.9,<0.4.0,>=0.2.43->langgraph) (2.3.0)\n",
      "Requirement already satisfied: sniffio>=1.1 in c:\\users\\rf.yin\\.conda\\envs\\pydantic-ai\\lib\\site-packages (from anyio->httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (1.3.1)\n"
     ]
    }
   ],
   "source": [
    "! pip install langgraph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGsAAADqCAIAAAAqMSwmAAAAAXNSR0IArs4c6QAAFvJJREFUeJztnXtAE1e6wE8yScg7hATC+yUiT9GKVi0KFnwWQUorVXHVtm5dWXfvtbt1d2tXu731eqnteu92W/eu2N2qW6vbKqW1oq1ixTdFLchL3k+BJOT9nuT+ES91S5KZMIk50Pn9x8yc4csvZyZnzjlzPorNZgMkBKD6OoAJD2mQKKRBopAGiUIaJAppkCg0guXVcrNSZtapUZ0KtZhtVusEaBshNECjUdl8hM2jCYPpbC4hCZTxtQdlA8a277QddVoGmwJsFDYPYfMRFodmRSeAQRqdolFZdCpUp7YY9VY6gxqbyolL4/JF9HGczW2DGoXlSoXUBoC/mB6TygkKZ47jv0LFQIe+vU47MmjiCmnzc8UMpnt3NvcM3jwrr7+inL9SPG0Wz/1QYaeuWnnlc+ncp0RpC/zxl3LDYPn7fXEzuclzBeONcGLw7Vdy2X3TkuJgnMfjrbFlr3XMfFI46fUBAGblBEQlcMrf78NbwIaDgzvbpf0GPEdOGu7dVh/b143nSOyruPz9vplPCiOnsT3w/U4oGq+r+tr1OWskrg/DMFhzTs7iIsnzJv/F65Car+QsDsbHd3Uf1CgsdZeVP1p9AID0nIALx4ddH+PK4JUK6fyVYk9HNcGYlyu6UiF1cYBTg7IBow2ASdnuc4tZ2UJpv9GgtTg7wKnBtu+0/uLxPOWMj/r6eqPR6KviruHwae31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+Oh8bmy0f/+fux2202GwA2hy2en216Lyx0mrMTKvpUS4rCnO19GLzjJKfe76eyec76F0wmg0YrH7vdYjHTaHSHRQT8IATx2DifswCsVqvNZnOYFZ3PC3QW20ivis81Z6/BNWCC16DsvrHir4PR6bi+lolOy6WuDTuj/Ni4niPwNuhFwX6Jc7jSdgff8yRjoGkoI1+MU597I02PLw1gMVHFgLee5GFA1qUIjaIlPe7GULjb48Wn/zZoRJnC0En4uzzcoQgOBwvy3Ju54PZj+YqNEopJK+tWuFsQcoZaZQK+xV194583U10u7e+y8IL5LN4jTb/iDbQjBp1UFTedNTNrPI3z8c/d6mrUfXNSijDoAVH+TK7Xn/O9gV5lknXI6QxbZqEoOGqc3U9E5w+21KrrrqhHBk28QDZHzKbREbofgtAhnUJonzxoMVvUQzr1sC44mjk9gx893nlvdjwzh1UpM3fUae93Gwe7DQYNyuLRdGro5rDS6VTUYmVyacHRzNBov5hUDmYeMDx45a0wi8mGotC9gkSjUxCa50ccYXyvbmIB79sQEwXSIFFIg0QhDRKFNEgU0iBR/g/omjlA0nvzEAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Assistant: 以下是一些顶级的科学期刊：\n",
      "\n",
      "1. 《自然》（Nature）：这是一份涵盖所有自然科学领域的国际性周刊，被认为是世界上最具影响力的科学期刊之一。\n",
      "\n",
      "2. 《科学》（Science）：这是一份由美国科学促进协会出版的同行评审科学期刊。它涵盖了各个学科的研究成果。\n",
      "\n",
      "3. 《细胞》（Cell）：这是一份专注于生物科学领域的国际性期刊，被认为是生物学领域最具影响力的期刊之一。\n",
      "\n",
      "4. 《美国科学院院报》（Proceedings of the National Academy of Sciences, PNAS）：这是美国国家科学院的官方期刊，涵盖了各个学科的研究成果。\n",
      "\n",
      "5. 《柳叶刀》（The Lancet）：这是一份专注于医学领域的国际性期刊，被认为是医学领域最具影响力的期刊之一。\n",
      "\n",
      "这些期刊都是在各自领域内具有高度声誉和影响力的期刊，发表在这些期刊上的论文通常被认为是高质量的研究成果。\n",
      "Assistant: 当然，以下是一些关于人工智能领域的顶级论文推荐：\n",
      "\n",
      "1. \"Human-level concept learning through probabilistic program induction\"（通过概率程序归纳实现人类水平的概念学习）- 作者：Josh Tenenbaum, Brendan Lake, and Tomer Ullman\n",
      "2. \"Playing Atari with Deep Reinforcement Learning\"（使用深度强化学习玩Atari游戏）- 作者：Volodymyr Mnih et al.\n",
      "3. \"ImageNet Classification with Deep Convolutional Neural Networks\"（使用深度卷积神经网络对ImageNet进行分类）- 作者：Alex Krizhevsky, Ilya Sutskever, and Geoffrey E. Hinton\n",
      "4. \"Deep Residual Learning for Image Recognition\"（用于图像识别的深度残差学习）- 作者：Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun\n",
      "5. \"Attention is All You Need\"（注意力就是你所需要的全部）- 作者：Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin\n",
      "\n",
      "这些论文涵盖了机器学习、深度学习和强化学习等重要领域。希望对你有所帮助！你可以通过搜索引擎或学术数据库如Google Scholar找到这些论文。\n",
      "Assistant: 系统学习人工智能（AI）是一个逐步的过程，需要掌握一系列的理论知识和实践技能。下面是一些建议帮助你开始：\n",
      "\n",
      "### 1. 打好数学基础\n",
      "\n",
      "- **线性代数**：向量、矩阵运算等。\n",
      "- **概率论与统计学**：理解随机变量、分布函数、期望值等概念。\n",
      "- **微积分**：包括一元和多元微积分，了解导数、积分等概念。\n",
      "\n",
      "### 2. 学习编程语言\n",
      "\n",
      "- 推荐学习Python，因为它是目前最广泛使用的AI开发语言之一，拥有丰富的库支持（如NumPy, Pandas, TensorFlow, PyTorch等）。\n",
      "\n",
      "### 3. 学习机器学习基础知识\n",
      "\n",
      "- **监督学习**：如线性回归、逻辑回归、决策树等。\n",
      "- **非监督学习**：如聚类分析、主成分分析等。\n",
      "- **强化学习**：通过与环境交互来学习最佳策略的方法。\n",
      "\n",
      "### 4. 深入学习深度学习\n",
      "\n",
      "- 理解神经网络的基本原理和工作方式。\n",
      "- 学习卷积神经网络（CNN）、循环神经网络（RNN）等高级模型。\n",
      "\n",
      "### 5. 实践项目经验\n",
      "\n",
      "- 尝试参与Kaggle竞赛或自己选择一个实际问题进行研究。\n",
      "- 使用开源数据集进行实验。\n",
      "\n",
      "### 6. 阅读最新的研究成果\n",
      "\n",
      "- 订阅AI领域的学术期刊和会议论文。\n",
      "- 加入相关的在线社区和论坛，与他人交流学习心得。\n",
      "\n",
      "### 7. 参加培训课程或获得学位\n",
      "\n",
      "- 在线课程平台（如Coursera、edX）提供许多优质的AI相关课程。\n",
      "- 考虑攻读计算机科学或相关领域的硕士/博士学位。\n",
      "\n",
      "### 8. 保持好奇心和持续学习的态度\n",
      "\n",
      "AI领域发展迅速，新技术层出不穷，保持对新知识的好奇心非常重要。\n",
      "\n",
      "通过以上步骤，你可以逐步建立起坚实的AI知识体系，并在实践中不断提高自己的技能水平。\n",
      "Assistant: Transformer模型是一种基于注意力机制的深度学习模型，它在自然语言处理（NLP）任务中表现出色。它首次被提出是在2017年，用于机器翻译任务。与循环神经网络（RNN）和长短时记忆网络（LSTM）等其他序列建模方法相比，Transformer模型使用自注意力机制（self-attention mechanism）来并行处理输入序列，从而大大提高了训练效率。\n",
      "\n",
      "Transformer模型的主要组成部分包括：\n",
      "\n",
      "1. 输入嵌入层（Input Embedding Layer）：将输入序列中的单词映射为向量表示。\n",
      "2. 位置编码层（Positional Encoding Layer）：将单词的位置信息添加到嵌入向量中，使模型能够理解词序。\n",
      "3. 多头注意力机制（Multi-Head Attention Mechanism）：允许模型关注输入序列的不同部分，并捕捉长距离依赖关系。\n",
      "4. 前馈神经网络（Feed-Forward Neural Network）：对每个位置的输出进行非线性变换。\n",
      "5. 残差连接和层归一化（Residual Connections and Layer Normalization）：这些技术有助于提高模型的训练效果。\n",
      "\n",
      "Transformer模型在各种NLP任务中取得了显著成果，如机器翻译、文本生成、问答系统和情感分析等。此外，基于Transformer架构的预训练模型，如BERT、GPT和T5等，在许多基准测试中达到了最先进的性能。\n",
      "Assistant: I'm not sure what you mean by \"111\". Could you please provide more context or ask a specific question? I'd be happy to help if you can clarify what you're looking for.\n",
      "Goodbye!\n"
     ]
    }
   ],
   "source": [
    "from typing import Annotated\n",
    "from typing_extensions import TypedDict\n",
    "\n",
    "from langgraph.graph import StateGraph, START, END\n",
    "from langgraph.graph.message import add_messages\n",
    "\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.messages import HumanMessage\n",
    "\n",
    "from IPython.display import Image, display\n",
    "\n",
    "base_url = \"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n",
    "api_key = \"sk-70a3c6b01ceb4b969063c9ca958a4f2e\"\n",
    "model_name = \"qwen-turbo-2024-11-01\"\n",
    "\n",
    "human_message = HumanMessage(content = '你好, 你是谁?')\n",
    "\n",
    "llm = ChatOpenAI(temperature=0, model_name=model_name, api_key=api_key, base_url=base_url)\n",
    "\n",
    "class State(TypedDict):\n",
    "    # 信息具有“list”类型。注释中的 \"add_messages\" 函数定义了应如何更新此状态键（在这种情况下，它将消息附加到列表中，而不是将其覆盖）。\n",
    "    messages: Annotated[list, add_messages]\n",
    "\n",
    "graph_builder = StateGraph(State)\n",
    "\n",
    "def chatbot(state: State):\n",
    "    return {\"messages\": [llm.invoke(state[\"messages\"])]}\n",
    "\n",
    "graph_builder.add_node(\"chatbot\", chatbot)\n",
    "\n",
    "graph_builder.add_edge(START, \"chatbot\")\n",
    "\n",
    "graph_builder.add_edge(\"chatbot\", END)\n",
    "\n",
    "graph = graph_builder.compile()\n",
    "\n",
    "try:\n",
    "    display(Image(graph.get_graph().draw_mermaid_png()))\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "\n",
    "def stream_graph_updates(user_input: str):\n",
    "    for event in graph.stream({\"messages\": [(\"user\", user_input)]}):\n",
    "        for value in event.values():\n",
    "            print(\"Assistant:\", value[\"messages\"][-1].content)\n",
    "\n",
    "while True:\n",
    "    try:\n",
    "        user_input = input(\"User: \")\n",
    "        if user_input.lower() in ['quit', 'exit', 'q']:\n",
    "            print(\"Goodbye!\")\n",
    "            break\n",
    "        stream_graph_updates(user_input)\n",
    "        pass\n",
    "    except:\n",
    "        user_input = \"你想知道什么关于LangGraph的问题 ? \"\n",
    "        print(\"User:\" + user_input)\n",
    "        stream_graph_updates(user_input)\n",
    "        break\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pydantic-ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
